﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Policy;
using System.Collections.Generic;

public partial class Controls_PageControl : System.Web.UI.UserControl
{
    private int _pageIndex = 0, _pageSize = 0, _pageCount = 0, _Counts = 0;
    private string _PrepareURL, _pageQueryName;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="pageIndex">第几页,大于等于1</param>
    /// <param name="pageSize">每页几条数据，大于0</param>
    /// <param name="Counts">总共几条数据</param>
    /// <param name="pageCounts">共几页,大于0</param>
    /// <param name="prepareUrl">转向的Url</param>
    /// <param name="pageQueryName">标识页的查询字符串</param>
    public void Bind(int pageIndex, int pageSize, int Counts, int pageCount, string prepareUrl, string pageQueryName)
    {
        _pageIndex = pageIndex;
        _pageSize = pageSize;
        _Counts = Counts;
        _pageCount = pageCount;
        _PrepareURL = prepareUrl;
        _pageQueryName = pageQueryName;
        Bind();
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="pageIndex">第几页</param>
    /// <param name="pageSize">每页几条数据</param>
    /// <param name="counts">总共几条数据</param>
    /// <param name="prepareUrl">转向的Url</param>
    /// <param name="pageQueryName">标识页的查询字符串</param>
    public void Bind(int pageIndex, int pageSize, int Counts, string prepareUrl, string pageQueryName)
    {
        _pageIndex = pageIndex;
        _pageSize = pageSize;
        _Counts = Counts;
        _pageCount = (Counts - 1) / pageSize + 1;
        _PrepareURL = prepareUrl;
        _pageQueryName = pageQueryName;
        Bind();
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private void Bind()
    {
        ddlGo.Attributes.Add("onchange", "javascript:location.href=this.value;");
        _PrepareURL = ReplaceParamentFromUrl(_PrepareURL, "page") + "page=";

        //显示状态
        if (_Counts > 0)
            ltStatus.Text = "第" + _pageIndex.ToString() + "页/共" + _pageCount.ToString() + "页,共" + _Counts.ToString() + "条记录";
        else
            ltStatus.Text = "未找到任何记录！";

        hlFirstPage.Enabled = hlPreviousPage.Enabled = _pageIndex > 1; //首页和前页链接是否可用
        hlLastPage.Enabled = hlNextPage.Enabled = _pageIndex < _pageCount; //末页和后页链接是否可用

        hlFirstPage.NavigateUrl = _PrepareURL + "1"; //首页链接

        int PageGo = _pageIndex - 1;
        hlPreviousPage.NavigateUrl = _PrepareURL + PageGo.ToString();//前页链接

        PageGo = _pageIndex + 1;
        hlNextPage.NavigateUrl = _PrepareURL + PageGo.ToString();//后页链接

        hlLastPage.NavigateUrl = _PrepareURL + _pageCount.ToString();//末页链接

        //为下拉框加上所有页的链接
        ddlGo.Items.Clear();
        for (int i = 1; i <= _pageCount; i++)
        {
            ListItem item = new ListItem("第" + i.ToString() + "页", _PrepareURL + i.ToString());
            ddlGo.Items.Add(item);
        }
        if (_pageIndex <= _pageCount && _pageIndex > 0)
            ddlGo.SelectedValue = _PrepareURL + _pageIndex.ToString();
    }


    private string ReplaceParamentFromUrl(string Url, string throwKey)
    {
        string newUrl = Url;

        if (Url.IndexOf('?') > 0)
        {
            newUrl = Url.Remove(Url.IndexOf('?'));
            newUrl += "?";
            Url = Url.Substring(Url.IndexOf('?') + 1);
            foreach (string parameter in Url.Split(new Char[] { '&' }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (parameter.Remove(parameter.IndexOf('=')) != throwKey)
                    newUrl += parameter + "&";
            }
        }
        else
            newUrl += "?";
        return newUrl;
    }
}
